package com.llpy.Service;

import com.llpy.dto.SlideDto;
import com.llpy.dto.SongDto;
import com.llpy.mapper.*;
import com.llpy.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ManageService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SlideMapper slideMapper;
    @Autowired
    private SongMapper songMapper;
    @Autowired
    private SingerMapper singerMapper;
    @Autowired
    private CommentService commentService;

    //查询所有用户
    public List<User> getAllUser(){
        List<User> all = userMapper.findAll();
        for (User user : all) {
            //gender的值为1返回男，为2返回女
            if(user.getGender().equals("1")){
                user.setGender("男");
            }else{
                user.setGender("女");
            }
        }
        return all;
    }

    //保存幻灯片路径
    public List<Slide> saveSlide(List<SlideDto> slides){
//        删除原本的幻灯片
        slideMapper.deleteAll();
        //遍历集合，存入新的幻灯片
        for (SlideDto slideDto : slides) {
            Slide slide = new Slide();
            slide.setUserImg(slideDto.getUserImg());
            slideMapper.save(slide);
        }
        return slideMapper.findAll();
    }


    //得到幻灯片路径，拿到全部幻灯片
    public List<Slide> getSlide(){
        return slideMapper.findAll();
    }

    //更改歌曲状态
    public boolean updateSong(String id){
        //得到该歌曲
        Optional<Song> byId = songMapper.findById(id);
        if (byId.isPresent()) {
            Song song = byId.get();
//            如果歌曲的root为1，则转为2，其他情况转为1。1为推荐
            if(song.getRoot()==1){
                song.setRoot(2);
                songMapper.save(song);
                return true;
            }else{
                song.setRoot(1);
                songMapper.save(song);
                return true;
            }
        }
        return false;
    }

    //获得所有推荐歌曲返回
    public Page<?> getSongs(Pageable pageable) {
        return songMapper.findByRoot(1,pageable);
    }

    //修改歌曲
    public boolean updateSong1(SongDto songDto) {
        Optional<Song> byId = songMapper.findById(songDto.getId());
        if (byId.isPresent()) {
            Song song = byId.get();
            //判断歌曲是否有歌手
            if(song.getSinger()!=null){
                Singer byName = singerMapper.findByName(songDto.getSingerName().trim());
                //歌手名不相同时进入
                if(!song.getSinger().getName().equals(songDto.getSingerName())){
                    //判断是否存在该歌手，不存在新建一个歌手
                    if(byName==null){
                        Singer singer = new Singer();
                        singer.setName(songDto.getSingerName());
                        singerMapper.save(singer);
                        songDto.setSinger(singer);
                        songMapper.save(songDto);
                        return true;
                    }else{
                        //存在将改歌手判断歌曲，如果不相同，将byName赋值给歌曲的singer字段并存入数据库
                        if(song.getName().equals(songDto.getName())){
                            return false;
                        }
                        songDto.setSinger(byName);
                        songMapper.save(songDto);
                        return true;
                    }
                }else{
                    //歌手名相同时，进来判断是否歌曲相同,相同返回false
                    if(song.getName().equals(songDto.getName())){
                        return false;
                    }
                    songDto.setSinger(byName);
                    songMapper.save(songDto);
                    return true;
                }
            }else{
                //没歌手就新建
                Singer singer = new Singer();
                singer.setName(songDto.getSingerName());
                System.out.println(singer);
                singerMapper.save(singer);
                songDto.setSinger(singer);
                songMapper.save(songDto);
                return true;
            }

        }
        return false;
    }

    //用户分页
    public Page<User> findUserAll(Pageable pageable) {
        Page<User> all = userMapper.findAll(pageable);
        for (User user : all) {
            if (user.getGender().equals("1")) {
                user.setGender("男");
            }else{
                user.setGender("女");
            }
        }
        return all;
    }

    //歌曲分页
    public Page<?> findSongAll(Pageable pageable) {
        return songMapper.findAll(pageable);
    }

    //评论分页
    public Page<?> findCommentAll(Pageable pageable) {
        return commentService.findAll(pageable);
    }

    //查询歌手或歌曲
    public List<SongDto> getSongOrSinger(String name) {
        List<SongDto> byName = songMapper.findByName(name);
        if(byName.size()>0){
            return byName;
        }else{
            Singer byName1 = singerMapper.findByName(name);
            if(byName1!=null){
                List<Song> bySinger = songMapper.findBySinger(byName1);
                List<SongDto> songDtos = new ArrayList<>();
                for (Song song : bySinger) {
                    songDtos.add((SongDto) song);
                }
                for (SongDto songDto : songDtos) {
                    songDto.setSingerName(songDto.getSinger().getName());
                }
                return songDtos;
            }
        }
        return null;
    }

    //查询指定评论
    public Page<Comment> findByNameRegex(String name1,Pageable pageable) {
        String regex = ".*" + name1 + ".*";
        return commentService.findByNameRegex(regex,pageable);
    }

    //模糊查询用户
    public Page<User> findByUserRegex(String name1,Pageable pageable) {
        String regex = ".*" + name1 + ".*";
        return userMapper.findByNicknameRegex(regex, pageable);
    }

    //模糊查询歌曲
    public Page<Song> findBySongRegex(String name1,Pageable pageable) {
        String regex = ".*" + name1 + ".*";
        return songMapper.findByNameRegex(name1,pageable);
    }

    public Page<SongDto> findBySingerRegex(String name1,Pageable pageable) {
        String regex = ".*" + name1 + ".*";
        return songMapper.findBySingerNameRegex(name1,pageable);
    }
}
